home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / palette / myfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  2.8 KB  |  112 lines

  1. //  myfile.cpp
  2. //
  3. //  Source file for Device-Independent Bitmap (DIB) API.  Provides
  4. //  the following functions:
  5. //
  6. //  ReadDIBFile()       - Loads a DIB from a file
  7. //
  8. //
  9. // This is a part of the Microsoft Foundation Classes C++ library.
  10. // Copyright (C) 1999 Microsoft Corporation
  11. // All rights reserved.
  12. //
  13. // This source code is only intended as a supplement to the
  14. // Microsoft Foundation Classes Reference and related
  15. // electronic documentation provided with the library.
  16. // See these sources for detailed information regarding the
  17. // Microsoft Foundation Classes product.
  18.  
  19. #include "stdafx.h"
  20. #include "dibapi.h"
  21. #include "mainfrm.h"
  22.  
  23. #include "resource.h"
  24.  
  25. /*
  26.  * Dib Header Marker - used in writing DIBs to files
  27.  */
  28. #define DIB_HEADER_MARKER   ((WORD) ('M' << 8) | 'B')
  29.  
  30. /*************************************************************************
  31.  
  32.   Function:  ReadDIBFile (CFile&)
  33.  
  34.    Purpose:  Reads in the specified DIB file into a global chunk of
  35.              memory.
  36.  
  37.    Returns:  A handle to a dib (hDIB) if successful.
  38.              NULL if an error occurs.
  39.  
  40.   Comments:  BITMAPFILEHEADER is stripped off of the DIB.  Everything
  41.              from the end of the BITMAPFILEHEADER structure on is
  42.              returned in the global memory handle.
  43.  
  44. *************************************************************************/
  45.  
  46. HDIB WINAPI ReadDIBFile(CFile& file)
  47. {
  48.     DWORD dwBitsSize;
  49.     HDIB hDIB;
  50.  
  51. #if defined(READ_BM_FROM_RESOURCE)
  52.     // determine location of the bitmap in resource fork
  53.     HINSTANCE hInst = AfxFindResourceHandle(MAKEINTRESOURCE(IDB_BITMAP), RT_BITMAP);
  54.     HRSRC hrsrc = ::FindResource(hInst, MAKEINTRESOURCE(IDB_BITMAP), RT_BITMAP);
  55.     if (hrsrc == NULL)
  56.         return FALSE;
  57.     dwBitsSize = SizeofResource(NULL, hrsrc);
  58.     if(dwBitsSize == 0)
  59.         return NULL;
  60.     HGLOBAL hGlob = LoadResource(NULL, hrsrc);
  61.     if(hGlob == NULL)
  62.         return NULL;
  63.  
  64.     hDIB = (HDIB)::GlobalAlloc(LMEM_FIXED | LMEM_ZEROINIT, dwBitsSize);
  65.     if (hDIB == 0)
  66.         return NULL;
  67.  
  68.     memcpy((void*)hDIB, (const void*)hGlob, dwBitsSize);
  69.  
  70. #else // READ_BM_FROM_RESOURCE
  71.  
  72.     BITMAPFILEHEADER bmfHeader;
  73.     LPSTR pDIB;
  74.     /*
  75.      * get length of DIB in bytes for use when reading
  76.      */
  77.  
  78.     dwBitsSize = file.GetLength();
  79.  
  80.     /*
  81.      * Go read the DIB file header and check if it's valid.
  82.      */
  83.     if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
  84.         return NULL;
  85.  
  86.     if (bmfHeader.bfType != DIB_HEADER_MARKER)
  87.         return NULL;
  88.  
  89.     /*
  90.      * Allocate memory for DIB
  91.      */
  92.     hDIB = (HDIB) ::GlobalAlloc(LMEM_FIXED | LMEM_ZEROINIT, dwBitsSize);
  93.     if (hDIB == 0)
  94.     {
  95.         return NULL;
  96.     }
  97.     pDIB = (LPSTR)hDIB;
  98.  
  99.     /*
  100.      * Go read the bits.
  101.      */
  102.     if (file.ReadHuge(pDIB, dwBitsSize - sizeof(BITMAPFILEHEADER)) != dwBitsSize - sizeof(BITMAPFILEHEADER) )
  103.     {
  104.         ::GlobalFree((HGLOBAL) hDIB);
  105.         return NULL;
  106.     }
  107. #endif // READ_BM_FROM_RESOURCE
  108.  
  109.     return hDIB;
  110. }
  111.  
  112.